package com.autosalon.ui.automobil.action;

import com.autosalon.DAO.DaoFactory;
import com.autosalon.ui.automobil.Automobil;
import com.autosalon.ui.automobil.state.AutomobilStateHolder;
import com.vaadin.ui.Button.ClickEvent;

/**
 *  Trida slouzici k mazani kategorii
 * @author Petr Kriegisch
 */
public class DeleteKategorielAction extends AbstractAutomobilAction {

    com.autosalon.model.Kategorie vybranaKategorie;

    public DeleteKategorielAction(Automobil automobil) {
        super(automobil);
    }

    @Override
    public void buttonClick(ClickEvent event) {
        int id;
        vybranaKategorie = (com.autosalon.model.Kategorie) DaoFactory.getinstance().getKategorie().getById(Long.parseLong(event.getButton().getData().toString()));
        try {
            DaoFactory.getinstance().getKategorie().delete(vybranaKategorie);
            id = automobil.getSwitcher().getMainWindow().getZakaznik().getRole().getId().intValue();
            automobil.getWindow().showNotification("Kategorie smazana");
            automobil.naplneniVsechTabulek();
            AutomobilStateHolder.setState(AutomobilStateHolder.VIEW_AUTOMOBIL_STATE, id, super.automobil);
        } catch (NullPointerException e) {
            id = 1;
            automobil.getWindow().showNotification("Smazani neprobehlo korektne");
        }


    }
}
